/**
 * 
 */
package com.ekingstar.framework.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;


/**
 * @author wpx
 *
 */
public class SpellUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] tt = getSpell("大乐");
		for (int i =0;i < tt.length;i++)
		System.out.println(tt[i]);
		System.out.println("匹配："+isSpellMatch("大乐","d"));

	}
	public static boolean isSpellMatch(String val,String express){
		boolean match = isMatch(val,express);
		if (match) return match;
		String[] spells = getSpell(val);
		for (int i =0 ;i < spells.length;i++){
			boolean ismatch = isMatch(spells[i],express);
			if (ismatch) return true;
		}
		return false;
	}
	public static boolean isMatch(String val,String express){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hedss?useUnicode=true&amp;characterEncoding=utf8","root","ws");
			PreparedStatement stat =  conn.prepareStatement("select count(*) from t_person where ? like ?");
			stat.setString(1, val);
			stat.setString(2, express+"%");
			ResultSet res =  stat.executeQuery();
			res.next();
			int count = res.getInt(1);
			res.close();
			stat.close();
			conn.close();
			return (count >=1);
		}catch(Throwable e){
			e.printStackTrace();
		}
		return false;
	}
	public static String[] getSpell(String val){
		try{
		List res = new ArrayList(val.length());
		char[] vals = val.toCharArray();
		for (int i =0 ;i < vals.length;i++){
			String[] spells = PinyinHelper.toHanyuPinyinStringArray(vals[i],format);
			Set ts = new HashSet();
			for (int j = 0;j < spells.length;j++){
				ts.add(spells[j]);
				ts.add(""+spells[j].charAt(0));
			}
			List t = new ArrayList();
			t.addAll(ts);
			res.add(i,t);
		}
		List res2 = combinatePlan(res);
		String[] retes = new String[res2.size()];
		for (int i =0 ;i < res2.size();i++){
			List temp = (List)res2.get(i);
			retes[i] = "";
			for (int j =0 ;j < temp.size();j++){
				String tt = (String)temp.get(j);
				retes[i] += tt;
			}
		}
		return retes;
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	private static List combinatePlan(List sourceList) {

		int[] subSizeArray;
		int[] subCountArray;
		int sourceCount = sourceList.size(); // 大list的长度
		subSizeArray = new int[sourceCount];
		subCountArray = new int[sourceCount];// 子list当前的值
		int targetCount = 1;
		for (int i = 0; i < sourceCount; i++) {
		List subList = (List) sourceList.get(i);
		int size = subList.size();
		subSizeArray[i] = size; // 存放每个子list的长度
		targetCount *= size; // 最终要返回的list长度
		subCountArray[i] = 0; // 从0开始 每个都是0
		// a.println("subCountArray[i]:"+subCountArray[i]);
		}

		List targetList = new ArrayList(targetCount);
		// targetCount
		for (int i = 0; i < targetCount; i++) {
		// 记录每个结果应该包含的元素个数
		List targetSub = new ArrayList(sourceCount);
		for (int k = 0; k < sourceCount; k++) {
		int subIndex = subCountArray[k];
		targetSub.add(((List) sourceList.get(k)).get(subIndex));
		}
		addCount(subSizeArray, subCountArray);
		targetList.add(targetSub);
		}

		return targetList;

		}

		/**
		* 计算List下标: 总是先数最后一位，最后一位数到最大值以后进一位
		* @param subSizeArray
		* @param subCountArray
		*/
		private static void addCount(int[] subSizeArray, int[] subCountArray) {

		// subCountArray数组的每个元素都是0 但是个数来记录循环要取的位置
		for (int i = subCountArray.length - 1; i >= 0; i--) {
		if (subCountArray[i] < subSizeArray[i] - 1) {
		subCountArray[i]++;
		return;
		} else {
		subCountArray[i] = 0;
		}
		}
		}


	private static HanyuPinyinOutputFormat format = null;
	static {
		format = new HanyuPinyinOutputFormat();
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
	}
}
